package com.example.crm.controller;

import com.example.crm.entity.SalesTeam;
import com.example.crm.entity.TeamMember;
import com.example.crm.entity.SalesTask;
import com.example.crm.service.SalesTeamService;
import com.example.crm.service.SalesTaskService;
import com.example.crm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sales/team")
public class SalesTeamController {

    @Autowired
    private SalesTeamService salesTeamService;
    
    @Autowired
    private SalesTaskService salesTaskService;
    
    /**
     * Display the sales team collaboration page
     */
    @GetMapping("/collaboration")
    public String collaborationPage(Model model) {
        // Load all teams for the dropdown
        List<SalesTeam> teams = salesTeamService.listTeams();
        model.addAttribute("teams", teams);
        
        // If there are teams, load the first team's details by default
        if (!teams.isEmpty()) {
            SalesTeam defaultTeam = teams.get(0);
            model.addAttribute("currentTeam", defaultTeam);
            
            // Load team members
            List<TeamMember> members = salesTeamService.listTeamMembers(defaultTeam.getId());
            model.addAttribute("teamMembers", members);
            
            // Load team tasks
            List<SalesTask> tasks = salesTaskService.getTasksByTeamId(defaultTeam.getId());
            model.addAttribute("teamTasks", tasks);
        }
        
        return "sales/collaboration";
    }
    
    /**
     * API endpoint to get all teams
     */
    @GetMapping("/api/teams")
    @ResponseBody
    public Result<List<SalesTeam>> listTeams() {
        try {
            List<SalesTeam> teams = salesTeamService.listTeams();
            return Result.success(teams);
        } catch (Exception e) {
            return Result.error("Failed to load teams: " + e.getMessage());
        }
    }
    
    /**
     * API endpoint to get team details
     */
    @GetMapping("/api/details/{teamId}")
    @ResponseBody
    public Result<Map<String, Object>> getTeamDetails(@PathVariable Long teamId) {
        try {
            // Get team details
            SalesTeam team = salesTeamService.getTeamDetail(teamId);
            
            // Get team members
            List<TeamMember> members = salesTeamService.listTeamMembers(teamId);
            
            // Get team tasks
            List<SalesTask> tasks = salesTaskService.getTasksByTeamId(teamId);

            /**
             * "team", team,
             *                 "members", members,
             *                 "tasks", tasks
             */
            // Create response data
            Map<String, Object> data =new HashMap<>();
            data.put("team", team);
            data.put("members", members);
            data.put("tasks", tasks);
            
            return Result.success(data);
        } catch (Exception e) {
            return Result.error("Failed to load team details: " + e.getMessage());
        }
    }
    
    /**
     * API endpoint to add a team message
     */
    @PostMapping("/api/message")
    @ResponseBody
    public Result<Boolean> addTeamMessage(@RequestBody Map<String, Object> params) {
        try {
            Long teamId = Long.valueOf(params.get("teamId").toString());
            Long userId = Long.valueOf(params.get("userId").toString());
            String message = params.get("message").toString();
            
            // Here you would add the message to your database
            // For now, we'll just return success
            
            return Result.success(true);
        } catch (Exception e) {
            return Result.error("Failed to send message: " + e.getMessage());
        }
    }
}